package com.heng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heng.dao.entity.coupon.CouponTemplateDO;
import com.heng.service.CouponTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/coupon-templates")
public class CouponTemplateController {

    @Autowired
    private CouponTemplateService couponTemplateService;

    // 新增优惠券模板
    @PostMapping
    public boolean addTemplate(@RequestBody CouponTemplateDO templateDO) {
        return couponTemplateService.save(templateDO);
    }

    // 删除模板（逻辑删除，delFlag=1）
    @DeleteMapping("/{id}")
    public boolean deleteTemplate(@PathVariable Long id) {
        return couponTemplateService.removeById(id);
    }

    // 更新模板
    @PutMapping
    public boolean updateTemplate(@RequestBody CouponTemplateDO templateDO) {
        return couponTemplateService.updateById(templateDO);
    }

    // 按ID查询模板
    @GetMapping("/{id}")
    public CouponTemplateDO getTemplateById(@PathVariable Long id) {
        return couponTemplateService.getById(id);
    }

    // 按店铺编号查询模板
    @GetMapping("/shop/{shopNumber}")
    public List<CouponTemplateDO> getByShopNumber(@PathVariable Long shopNumber) {
        return couponTemplateService.getByShopNumber(shopNumber);
    }

    // 查询所有生效中的模板
    @GetMapping("/valid")
    public List<CouponTemplateDO> getValidTemplates() {
        return couponTemplateService.getValidTemplates();
    }

    // 分页查询模板
    @GetMapping("/page")
    public IPage<CouponTemplateDO> pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return couponTemplateService.pageQuery(pageNum, pageSize);
    }
}